package top.jnaw.jee.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.util.List;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.platform.model.Approval;
import top.jnaw.jee.platform.model.Files;
import top.jnaw.jee.platform.model.MessagePush;
import top.jnaw.jee.platform.model.Users;

/**
 * Copyright (C) 2018 版权所有。 <p> 文件名：Approval 文件功能描述：审批工具类
 *
 * @author lc  2018/9/27 9:37 创建内容： <p> <p> 修改标识： 修改原因：
 */
public class ApprovalUtil {

  public static void urge(int aid) {
    List<Users> usersList = Approval.getCurrentApprovers(aid)
        .toJavaList(Users.class);

    Log.e("usersList.size():" + usersList.size());
    if (usersList.size() > 0) {
      String title = "审批申请";
      String alert = String
          .format(Consts.NIM_NOTIFY_TEMPLATE_UNREAD, "审批");
      String from = "approval";
      MessagePush messagePush = new MessagePush(title, alert, from,
          "", String.valueOf(aid), "", "", usersList);
      messagePush.sendJPush(true);
      messagePush.sendNIM(true);
    }
  }

  public static JSONArray charge(String data) {
    JSONArray array = new JSONArray();
    if (StrKit.notBlank(data)) {
      array = JSON.parseArray(data);
      if (array.size() > 0) {
        for (int i = 0; i < array.size(); i++) {
          JSONObject object = array.getJSONObject(i);
          JSONArray accessory = object.getJSONArray("accessory");
          JSONArray acc = new JSONArray();
          if (null != accessory && accessory.size() > 0) {
            for (int j = 0; j < accessory.size(); j++) {
              int fid = accessory.getIntValue(j);
              if (fid > 0) {
                String sql = Db.getSql("files-list")
                    .replace("f.uid =?", " f.id = " + fid);
                Record record = Db.findFirst(sql);
                acc.add(record.getColumns());
              }
            }
          }
          object.put("accessory", acc);
        }
      }
    }
    return array;
  }
}
